<template>
  <div class="login">
    <van-nav-bar placeholder @click-right="showUser = !showUser" :border='false'
      style="background-color: rgba(0, 0, 0, 0);" fixed left-arrow>
      <template #left>
        <van-button size="small" replace @click="onrouter" round type="default" icon="arrow-left">
        </van-button>
      </template>
      <template #right>
        <van-button v-if="showUser" style="background-color: #18aeff;color: antiquewhite;" size="small" round
          type="default">登录</van-button>
        <van-button v-if="!showUser" style="background-color: #18aeff;color: antiquewhite;" size="small" round
          type="default">注册</van-button>
      </template>
    </van-nav-bar>
    <div v-if="!showUser" class="denglu">
      <van-cell>
        <template #title>
          <div style="width: 100%;text-align: center;font-weight: 600;font-size: 1.5rem;">登录</div>
        </template>
      </van-cell>
      <van-form @submit="onLogin">
        <van-field v-model="username" name="id" label="用户名" placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]" />
        <van-field v-model="password" type="password" name="pwd" label="密码" placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]" />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit">提交</van-button>
        </div>
      </van-form>
    </div>
    <div v-if="showUser" class="zhuce">
      <van-cell>
        <template #title>
          <div style="width: 100%;text-align: center;font-weight: 600;font-size: 1.5rem;">注册</div>
        </template>
      </van-cell>
      <van-form @submit="onZhuce">
        <van-field v-model="usernick" name="nick" label="昵称" placeholder="昵称"
          :rules="[{ required: true, message: '请填写用户名' }]" />
        <van-field v-model="username" name="id" label="用户名" placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]" />
        <van-field v-model="password" type="password" name="pwd" label="密码" placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]" />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit">提交</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
  import router from '@/router'
  export default {
    name: 'logIn',
    data() {
      return {
        showUser: false,
        usernick: '',
        username: '',
        password: ''
      }
    },
    methods: {
      async onLogin(values) {
        const res = await this.$api.homeData.user_login(values)
        if (res.status === 200) {
          router.push('Home')
          localStorage.setItem('userToken', res.data)
        } else {
          this.$toast(res.message)
        }
      },
      async onZhuce(values) {
        const res = await this.$api.homeData.user_login(values)
        if (res.status === 201) {
          router.push('Home')
          localStorage.setItem('userToken', res.data)
        } else {
          this.$toast(res.message)
        }
      },
      onrouter() {
        router.push('Home')
      }
    }
  }
</script>

<style>
</style>
